package com.yuews.codegenerator.model;

/**
 * 字段信息模型
 */
public class ColumnInfo {
    private String columnName;      // 字段名
    private String dataType;        // 数据类型
    private String columnComment;   // 字段注释
    private boolean primaryKey;     // 是否为主键
    private boolean notNull;        // 是否非空
    private String defaultValue;    // 默认值
    private boolean autoIncrement;  // 是否自增
    private int length;             // 长度

    // Getters and Setters
    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getColumnComment() {
        return columnComment;
    }

    public void setColumnComment(String columnComment) {
        this.columnComment = columnComment;
    }

    public boolean isPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(boolean primaryKey) {
        this.primaryKey = primaryKey;
    }

    public boolean isNotNull() {
        return notNull;
    }

    public void setNotNull(boolean notNull) {
        this.notNull = notNull;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public boolean isAutoIncrement() {
        return autoIncrement;
    }

    public void setAutoIncrement(boolean autoIncrement) {
        this.autoIncrement = autoIncrement;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    /**
     * 获取Java属性名（驼峰命名）
     */
    public String getPropertyName() {
        return underlineToCamelCase(columnName, false);
    }

    /**
     * 获取Java数据类型
     */
    public String getJavaType() {
        return mapSqlTypeToJavaType(dataType);
    }

    /**
     * 下划线转驼峰命名
     */
    private String underlineToCamelCase(String str, boolean capitalizeFirst) {
        if (str == null || str.isEmpty()) {
            return str;
        }
        
        StringBuilder result = new StringBuilder();
        boolean nextUpperCase = capitalizeFirst;
        
        for (char c : str.toCharArray()) {
            if (c == '_') {
                nextUpperCase = true;
            } else {
                if (nextUpperCase) {
                    result.append(Character.toUpperCase(c));
                    nextUpperCase = false;
                } else {
                    result.append(Character.toLowerCase(c));
                }
            }
        }
        
        return result.toString();
    }

    /**
     * SQL类型映射到Java类型
     */
    private String mapSqlTypeToJavaType(String sqlType) {
        sqlType = sqlType.toUpperCase();
        
        if (sqlType.contains("INT") || sqlType.contains("NUMBER") && sqlType.contains("(10)")) {
            return "Integer";
        } else if (sqlType.contains("BIGINT") || sqlType.contains("NUMBER") && sqlType.contains("(19)")) {
            return "Long";
        } else if (sqlType.contains("VARCHAR") || sqlType.contains("CHAR")) {
            return "String";
        } else if (sqlType.contains("DATE") || sqlType.contains("TIME")) {
            return "Date";
        } else if (sqlType.contains("DECIMAL") || sqlType.contains("FLOAT") || sqlType.contains("DOUBLE")) {
            return "BigDecimal";
        } else if (sqlType.contains("BOOLEAN") || sqlType.contains("BIT")) {
            return "Boolean";
        } else if (sqlType.contains("BLOB") || sqlType.contains("BINARY")) {
            return "byte[]";
        } else if (sqlType.contains("CLOB") || sqlType.contains("TEXT")) {
            return "String";
        }
        
        // 默认返回String
        return "String";
    }

    /**
     * 获取 getter 方法名
     */
    public String getGetterMethod() {
        String propertyName = getPropertyName();
        return "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
    }

    /**
     * 获取 setter 方法名
     */
    public String getSetterMethod() {
        String propertyName = getPropertyName();
        return "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
    }
}